home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / Picture.C < prev    next >
C/C++ Source or Header  |  1992-07-10  |  6KB  |  256 lines

  1. #ifdef __GNUG__
  2. #pragma implementation
  3. #endif
  4.  
  5. #include "Picture.h"
  6.  
  7. #include "Class.h"
  8. #include "String.h"
  9. #include "ObjArray.h"
  10. #include "WindowSystem.h"
  11. #include "DevBitmap.h"
  12. #include "PictPort.h"
  13.  
  14. //---- Picture -----------------------------------------------------------------
  15.  
  16. NewMetaImpl(Picture,Object, (TV(buf,length), T(bbox), TP(map)));
  17.  
  18. Picture::Picture()
  19. {
  20.     buf= 0;
  21.     map= 0;
  22. }
  23.  
  24. Picture::Picture(Rectangle &b, byte *pagebuf, long len, ObjArray *m)
  25. {
  26.     Set(b, pagebuf, len, m);
  27. }
  28.  
  29. Picture::~Picture()
  30. {
  31.     SafeDelete(buf);
  32.     SafeDelete(map);
  33. }
  34.  
  35. void Picture::Set(Rectangle &b, byte *pagebuf, long len, ObjArray *m)
  36. {
  37.     SafeDelete(buf);
  38.     SafeDelete(map);
  39.     map= m;
  40.     length= (int) len;
  41.     bbox= b;
  42.     if (length > 0) {
  43.     buf= new byte[length];
  44.     memcpy(buf, pagebuf, (u_int) length);
  45.     } else
  46.     buf= 0;
  47. }
  48.  
  49. OStream &Picture::PrintOn(OStream &os)
  50. {
  51.     Object::PrintOn(os);
  52.     os << bbox SP << map SP;
  53.     return os.PrintHexString(buf, length);
  54. }
  55.  
  56. IStream &Picture::ReadFrom(IStream &is)
  57. {
  58.     int l;
  59.     Object::ReadFrom(is);
  60.     is >> bbox >> map;
  61.     is.ReadString(&buf, &l);
  62.     length= l;
  63.     return is;
  64. }
  65.  
  66. static Point GetPoint(IStream &is, byte code)
  67. {
  68.     Point p;
  69.     
  70.     switch ((code & 0x0c) >> 2) {
  71.     case 0:
  72.     break;
  73.     case 1:
  74.     p.x= is.GetChar();
  75.     break;
  76.     case 2:
  77.     p.x= (short) is.GetBigEndian(2);
  78.     break;
  79.     case 3:
  80.     p.x= (int) is.GetBigEndian(4);
  81.     break;
  82.     }
  83.     switch (code & 0x03) {
  84.     case 0:
  85.     break;
  86.     case 1:
  87.     p.y= is.GetChar();
  88.     break;
  89.     case 2:
  90.     p.y= (short) is.GetBigEndian(2);
  91.     break;
  92.     case 3:
  93.     p.y= (int) is.GetBigEndian(4);
  94.     break;
  95.     }
  96.     return p;
  97. }
  98.  
  99. void Picture::Show(Rectangle *rp, register Port *port)
  100. {
  101.     register int i;
  102.     Rectangle r;
  103.     Ink *pat= gInkBlack;
  104.     byte b, hintbuf[1000];
  105.     GrLineCap cap= eDefaultCap;
  106.     GrPolyType polytype;
  107.     Picture *pict;
  108.     Font *font= gSysFont;
  109.     int lastlen= 0, psz= 1, startangle= 0, endangle= 0, code, len;
  110.     Point dia, p, *pts= 0;
  111.     Bitmap *bm;
  112.     bool done= FALSE;
  113.     MemBuf mb(length, (char*)buf, length);
  114.     IStream is(&mb);
  115.     
  116.     GrState gr(port);
  117.  
  118.     port->Scale(1.0/port->GetXScale(), 1.0/port->GetYScale());
  119.     port->Translate(rp->origin-bbox.origin-port->GetOrigin());
  120.     port->Scale((float) rp->extent.x / (float) bbox.extent.x,
  121.         (float) rp->extent.y / (float) bbox.extent.y);
  122.         
  123.     while (!done && is.get(b)) {
  124.     switch (b) {
  125.     case cPicEnd:
  126.         done= TRUE;
  127.         continue;
  128.     case cPicClip:
  129.         port->Clip(gr.clip);
  130.         port->ClipFurther(r);
  131.         continue;
  132.     case cPicFont:
  133.         if ((font= (Font*) map->At((short) is.GetBigEndian(2))) == 0)
  134.         font= gSysFont;
  135.         continue;
  136.     case cPicInk:
  137.         if ((pat= (Ink*) map->At((short) is.GetBigEndian(2))) == 0)
  138.         pat= gInkBlack;
  139.         continue;
  140.     case cPicStartAngle:
  141.         startangle= (short) is.GetBigEndian(2);
  142.         continue;
  143.     case cPicEndAngle:
  144.         endangle= (short) is.GetBigEndian(2);
  145.         continue;
  146.  
  147.     case cPicExtent: case cPicExtent+1: case cPicExtent+2: case cPicExtent+3:
  148.     case cPicExtent+4: case cPicExtent+5: case cPicExtent+6: case cPicExtent+7:
  149.     case cPicExtent+8: case cPicExtent+9: case cPicExtent+10: case cPicExtent+11:
  150.     case cPicExtent+12: case cPicExtent+13: case cPicExtent+14: case cPicExtent+15:
  151.         r.extent+= GetPoint(is, b);
  152.         continue;
  153.  
  154.     case cPicMove: case cPicMove+1: case cPicMove+2: case cPicMove+3:
  155.     case cPicMove+4: case cPicMove+5: case cPicMove+6: case cPicMove+7:
  156.     case cPicMove+8: case cPicMove+9: case cPicMove+10: case cPicMove+11:
  157.     case cPicMove+12: case cPicMove+13: case cPicMove+14: case cPicMove+15:
  158.         r.origin+= GetPoint(is, b);
  159.         continue;
  160.  
  161.     case cPicLine: case cPicLine+1: case cPicLine+2: case cPicLine+3:
  162.     case cPicLine+4: case cPicLine+5: case cPicLine+6: case cPicLine+7:
  163.     case cPicLine+8: case cPicLine+9: case cPicLine+10: case cPicLine+11:
  164.     case cPicLine+12: case cPicLine+13: case cPicLine+14: case cPicLine+15:
  165.         p= r.origin;
  166.         r.origin+= GetPoint(is, b);
  167.         port->StrokeLine(pat, psz, cap, p, r.origin);
  168.         break;
  169.     
  170.     case cPicCornerDia: case cPicCornerDia+1: case cPicCornerDia+2: case cPicCornerDia+3:
  171.     case cPicCornerDia+4: case cPicCornerDia+5: case cPicCornerDia+6: case cPicCornerDia+7:
  172.     case cPicCornerDia+8: case cPicCornerDia+9: case cPicCornerDia+10: case cPicCornerDia+11:
  173.     case cPicCornerDia+12: case cPicCornerDia+13: case cPicCornerDia+14: case cPicCornerDia+15:
  174.         dia= GetPoint(is, b);
  175.         break;
  176.  
  177.     case cPicPensize:
  178.         psz= is.GetByte();
  179.         continue;
  180.     case cPicPenCap:
  181.         cap= (GrLineCap) is.GetByte();
  182.         continue;
  183.     case cPicStrokeRect:
  184.         port->StrokeRect(pat, psz, r);
  185.         continue;
  186.     case cPicStrokeRRect:
  187.         port->StrokeRRect(pat, psz, r, dia);
  188.         continue;
  189.     case cPicStrokeOval:
  190.         port->StrokeOval(pat, psz, r);
  191.         continue;
  192.     case cPicStrokeWedge:
  193.         port->StrokeWedge(pat, psz, cap, r, startangle, endangle);
  194.         continue;
  195.     case cPicFillPoly:
  196.     case cPicStrokePoly:
  197.         polytype= (GrPolyType) is.GetByte();
  198.         len= (short) is.GetBigEndian(2);
  199.         if (len > lastlen) {
  200.         SafeDelete(pts);
  201.         pts= new Point[lastlen= len];
  202.         }
  203.         for (i= 0; i < len; i++)
  204.         pts[i]= GetPoint(is, is.GetByte());
  205.         if (b == cPicStrokePoly)
  206.         port->StrokePolygon(r.origin, pat, pts, len, polytype, psz, cap);
  207.         else
  208.         port->FillPolygon(r.origin, pat, pts, len, polytype);
  209.         continue;
  210.     case cPicFillRect:
  211.         port->FillRect(pat, r);
  212.         continue;
  213.     case cPicFillOval:
  214.         port->FillOval(pat, r);
  215.         continue;
  216.     case cPicFillRRect:
  217.         port->FillRRect(pat, r, dia);
  218.         continue;
  219.     case cPicFillWedge:
  220.         port->FillWedge(pat, r, startangle, endangle);
  221.         continue;
  222.     case cPicShowBitmap:
  223.         if (bm= (Bitmap*) map->At((short) is.GetBigEndian(2)))
  224.         port->ShowBitmap(pat, r, bm);
  225.         continue;
  226.     case cPicShowPicture:
  227.         if (pict= (Picture*) map->At((short) is.GetBigEndian(2)))
  228.         port->ShowPicture(r, pict);
  229.         continue;
  230.     case cPicHint:
  231.         code= (int) is.GetBigEndian(4);
  232.         len= (int) is.GetBigEndian(4);
  233.         for(i= 0; i < len; i++)
  234.         is.get(hintbuf[i]);
  235.         port->GiveHint(code, len, len > 0 ? hintbuf : 0);
  236.         continue;
  237.     case cPicEsc:
  238.         r.origin.x+= port->ShowChar(font, pat, r.origin, is.GetByte());
  239.         continue;
  240.     default:
  241.         if (b >= cPicXMove0 && b < cPicXMove0+64)
  242.         r.origin.x+= b-cPicXMove0-32;
  243.         else
  244.         r.origin.x+= port->ShowChar(font, pat, r.origin, b);
  245.         continue;
  246.     }
  247.     }
  248. }
  249.  
  250. void Picture::CollectParts(Collection *col)
  251. {
  252.     if (map)
  253.     col->Add(map);
  254. }
  255.  
  256.